import $pacsApi from '../../api/pacs'

const state = {
  imgSendAlertShow: false,
  imgSendAlertBtnShow: false,
  completeCount: 0,
  totalCount: 0,
  timeId: null
}

const mutations = {
  SET_ALERT_SHOW (state, payload) {
    console.log('state, payload', state, payload)
    state.imgSendAlertShow = payload
  },
  SET_ALERT_BTN_SHOW (state, payload) {
    console.log('state, payload', state, payload)
    state.imgSendAlertBtnShow = payload
  },
  CLEAR_TIME_ID (state) {
    clearInterval(state.timeId)
    state.timeId = null
  },
  SET_COMPLETE_COUNT (state, payload) {
    state.completeCount = payload
  },
  SET_TOTAL_COUNT (state, payload) {
    state.totalCount = payload
  }
}

const actions = {
  pollingGetSendProgressRate({ commit, state }) { // 轮询获取影像发送进度
    if(state.timeId) { // 目前有进行中的定时任务
      commit('CLEAR_TIME_ID') // 清除定时器
    }else {
      state.timeId = setInterval(async () => {
        let res = await $pacsApi.pacsApi.getSendProgressRate()
        let { code, data } = res
        if(code === 0) {
          if(data.total_count === -1) { // 没有进行中的任务，返回-1
            commit('SET_COMPLETE_COUNT', 0)
            commit('SET_TOTAL_COUNT', 0)
            commit('CLEAR_TIME_ID') // 清除定时器
          }else {
            commit('SET_COMPLETE_COUNT', data.complete_count)
            commit('SET_TOTAL_COUNT', data.total_count)
          }
        }
      }, 3000)
    }
  }
  // pollingGetSendProgressRate({ commit, state }) { // 轮询获取影像发送进度
  //   if(state.timeId) { // 目前有进行中的定时任务
  //     commit('CLEAR_TIME_ID') // 清除定时器
  //   }else {
  //     state.timeId = setInterval(async () => {
  //       commit('SET_COMPLETE_COUNT', state.completeCount + 0.5)
  //       commit('SET_TOTAL_COUNT', 100)
  //     }, 100)
  //   }
  // }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
